package com.lxd.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.lxd.entity.NewsRequest;
import com.lxd.entity.po.File;
import com.lxd.entity.po.User;
import com.lxd.entity.query.FileQuery;
import com.lxd.entity.query.UserQuery;
import com.lxd.entity.vo.NewsVO;
import com.lxd.mappers.FileMapper;
import com.lxd.mappers.UserMapper;
import com.lxd.utils.CopyTools;
import org.springframework.stereotype.Service;

import com.lxd.entity.enums.PageSize;
import com.lxd.entity.query.NewsQuery;
import com.lxd.entity.po.News;
import com.lxd.entity.vo.PaginationResultVO;
import com.lxd.entity.query.SimplePage;
import com.lxd.mappers.NewsMapper;
import com.lxd.service.NewsService;
import com.lxd.utils.StringTools;


/**
 * 新闻表 业务接口实现
 */
@Service("newsService")
public class NewsServiceImpl implements NewsService {

	@Resource
	private NewsMapper<News, NewsQuery> newsMapper;
	@Resource
	private UserMapper<User, UserQuery> userMapper;
	@Resource
	private FileMapper<File, FileQuery> fileMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<News> findListByParam(NewsQuery param) {
		List<News> news = newsMapper.selectList(param);
		return news;
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(NewsQuery param) {
		return this.newsMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<NewsVO> findListByPage(NewsQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);

		param.setOrderBy("hot desc,updated_at desc");
		List<News> list = this.findListByParam(param);
		List<NewsVO> vos = CopyTools.copyList(list, NewsVO.class);
		for (NewsVO vo : vos) {
			vo.setImageUrl(getFilePath(vo.getId(), 1));
		}
		PaginationResultVO<NewsVO> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), vos);
		return result;
	}

	/**
	 * 获取关联内容的最新图片
	 * @param relatedId 关联ID
	 * @param relatedType 关联类型（0：活动，1：新闻）
	 * @return 图片URL
	 */
	private String getFilePath (Long relatedId, Integer relatedType ){
		File file = fileMapper.selectLatestByRelatedId(relatedId, relatedType);
		return file == null ? "" : file.getFilePath();
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(News bean) {
		return this.newsMapper.insert(bean);
	}
	@Override
	public Integer Adminadd(NewsRequest newsRequest) {
		Date date = new Date();
		News news = CopyTools.copy(newsRequest, News.class);
		news.setCreatedAt(date);
		news.setUpdatedAt(date);
		return this.newsMapper.insert(news);
	}

    @Override
    public Integer getTotalCount() {
        return newsMapper.selectCount(new NewsQuery());
    }

    /**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<News> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.newsMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<News> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.newsMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(News bean, NewsQuery param) {
		StringTools.checkParam(param);
		return this.newsMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(NewsQuery param) {
		StringTools.checkParam(param);
		return this.newsMapper.deleteByParam(param);
	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public News getNewsById(Long id) {
		return this.newsMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateNewsById(NewsRequest newsRequest) {

		//将newsRequest属性赋值给news
		News news = CopyTools.copy(newsRequest, News.class);
		return this.newsMapper.updateById(news, newsRequest.getId());
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteNewsById(Long id) {
		return this.newsMapper.deleteById(id);
	}


}